<template>
  <div class="serach-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="this.$route.query.keyword+'的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
     >
      <van-cell
        v-for="item in articleData"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push('/article/'+item.art_id)"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { getSearch } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      articleData: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  methods: {
    async onLoad () {
      // 根据路由传参获取文章详情
      const { data } = await getSearch({ q: this.$route.query.keyword, page: this.page })
      this.articleData.push(...data.results)
      this.loading = false
      if (!data.results.length) this.finished = true
      this.page++
    }
  }
}
</script>

<style lang="less" scoped>
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
